import 'dart:io';       // Directory所在的包
import 'package:path/path.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

import 'util.dart';

class PathUtil{
  static Future<Directory> get tempDir async {
    return await getTemporaryDirectory();
  }

  static Future<Directory> get docDir async {
    return await getApplicationDocumentsDirectory();
  }

  static Future<String> assetPath(String assetName) async {
    return join((await tempDir).path, assetName);
  }

  static Future<File> assetToFile(String assetName) async {
    final ByteData assetByteData = await rootBundle.load('assets/img/$assetName');

    final List<int> byteList = assetByteData.buffer
        .asUint8List(assetByteData.offsetInBytes, assetByteData.lengthInBytes);

    final String fullTemporaryPath =
    join((await tempDir).path, assetName);

    Future<File> fileFuture = new File(fullTemporaryPath)
        .writeAsBytes(byteList, mode: FileMode.writeOnly, flush: true);

    ffprint('assets/$assetName saved to file at $fullTemporaryPath.');

    return fileFuture;
  }

  static void assetToPipe(String assetName, String pipePath) async {
    final ByteData byteData = await rootBundle.load('assets/$assetName');

    var pipeFile = new File(pipePath);
    await pipeFile.writeAsBytes(
        byteData.buffer
            .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes),
        mode: FileMode.writeOnly,
        flush: false);

    ffprint('assets/$assetName saved to pipe at $pipePath.');
  }
}